package com.person.commons.utils;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.apache.log4j.Logger;

public class FileUtils {

	/* 记录压缩进度 */
	private int zipProgress = -1;

	private static Logger log = Logger.getLogger(FileUtils.class);

	private boolean isStopZip = false;

	public boolean isStopZip() {
		return isStopZip;
	}

	public void setStopZip(boolean isStopZip) {
		this.isStopZip = isStopZip;
	}

	public FileUtils() {
	}

	public int getZipProgress() {
		return zipProgress;
	}

	/* 采用递归方式，遍觅指定文件夹下的所有文件集合。 */
	public ArrayList getAllFile(ArrayList fileList) {
		ArrayList newList = new ArrayList();

		for (int nJ = 0; nJ < fileList.size(); nJ++) {
			File file = (File) fileList.get(nJ);
			if (file.exists()) {
				if (file.isFile()) {
					newList.add(file);
				} else {
					// newList.add(file); 只添加文件入集合，不添加文件夹
					File[] arrFile = file.listFiles();
					ArrayList list = new ArrayList();
					for (int nI = 0; nI < arrFile.length; nI++) {
						list.add(arrFile[nI]);
					}
					newList.addAll(getAllFile(list));
				}
			}
		}
		return newList;
	}

	/* 压缩文件集合fileList至目标文件targetFileName */
	public boolean fileZip(List fileList, String targetFileName) {
		FileInputStream in = null;
		FileOutputStream out = null;
		ZipOutputStream zipOut = null;
		try {
			/* 创建文件输出流对象 */
			out = new FileOutputStream(targetFileName);

			/* 创建ZIP数据输出流对象 */
			zipOut = new ZipOutputStream(out);
			ZipEntry entry;
			int nNumber;

			/* 循环写入压缩文件 */
			for (int i = 0; i < fileList.size(); i++) {

				/* 判断用户是否取消了压缩 */
				if (isStopZip) {
					return false;
				}

//				/* 根据文件数目的比例，计算压缩进度 */
//				Double dbProgress = new Double(i * 100) / fileList.size();
//				this.zipProgress = dbProgress.intValue();
//				System.out.println("progress:" + this.zipProgress);
//
				/* 从集合中提取文件对象，获得其在压缩文件中的相对路径 */
				File file = (File) fileList.get(i);
				String fileName = file.getPath();
//				fileName = fileName.substring(fileName.indexOf("bloguser"));

				/* 创建文件输入流对象 */
				in = new FileInputStream(file);

				/* 创建指向压缩原始文件的入口 */
				entry = new ZipEntry(fileName);
				zipOut.putNextEntry(entry);

				/* 向压缩文件中输出数据 */
				byte[] buffer = new byte[2048];
				while ((nNumber = in.read(buffer)) != -1) {
					zipOut.write(buffer, 0, nNumber);
				}
				in.close();
			}
			this.zipProgress = 100;
			return true;
		} catch (IOException e) {
			log.info(e.getMessage());

			/* 创建目标文件对象，当出现错误时，删除该文件 */
			File file = new File(targetFileName);
			if (file.exists()) {
				file.delete();
			}

			return false;
		} finally {
			if (isStopZip) {
				/* 创建目标文件对象，当出现错误时，删除该文件 */
				File file = new File(targetFileName);
				if (file.exists()) {
					file.delete();
				}
			}

			try {
				if (zipOut != null) {
					zipOut.close();
				}
				if (out != null) {
					out.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				log.info(e.getMessage());
				return false;
			}
		}
	}
}